//
//  SelectPlanViewController.m
//  Wallets Manager
//
//  Created by Tung Nguyen on 10/14/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "SelectPlanViewController.h"

@interface SelectPlanViewController ()

@end

@implementation SelectPlanViewController
@synthesize seletedPlanTableView;
@synthesize isFromView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //---------------------------Load table view-------------------//
    
    folyAppDelegate *delegate = (folyAppDelegate *)[[UIApplication sharedApplication]delegate];
    planArray = [NSMutableArray arrayWithArray:[DataBaseAccess getPlanByStatus:NO andWallet:delegate.currentWallet]];
    for (Plan *plan in planArray) {
        if([plan.p_name isEqualToString:@"Chưa có kế hoạch"]){
            [planArray removeObject:plan];
            break;
        }
    }
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [self setSeletedPlanTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)viewWillAppear:(BOOL)animated {
    
    
    //[sele reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - TableView Delegate
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  [planArray count];
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return  1;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,320,40)];
    headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sub_navbar_bg.png"]];
    
    UILabel *sectionTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, -5, 100, 30)];
    sectionTitleLabel.text = @"Running";
    sectionTitleLabel.textColor = [UIColor whiteColor];
    sectionTitleLabel.font = [UIFont systemFontOfSize:14];
    sectionTitleLabel.backgroundColor = [UIColor clearColor];
    [headerView addSubview:sectionTitleLabel];
    
    return headerView;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomPlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellInditifier"] ;
    Plan * tempPlan = [planArray objectAtIndex:indexPath.row];
  
    
    if( cell == nil) {
        
        //UIViewController *viewForCell = [[UIViewController alloc]initWithNibName:@"CustomPlanCell" bundle:nil] ;
        NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"CustomPlanCell" owner:self options:nil];
        
        cell = (CustomPlanCell *)[nibViews objectAtIndex:0];
        //cell = (CustomPlanCell *) viewForCell.view;
        //[cell addGestureRecognizer:longTap];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.planNameLabel.text = tempPlan.p_name;
    cell.imageView.image = [UIImage imageNamed:tempPlan.p_image];
    cell.startAmountLabel.text = [NSString stringWithFormat:@"%d",[tempPlan.p_startamount intValue]];
    
    //Set Width for StartAmount to Fit StartAmount TextField Background
    NSString *amountString =[NSString stringWithFormat:@"%f",[tempPlan.p_amount doubleValue]];
    CGSize textLabelSize = [amountString sizeWithFont:[UIFont systemFontOfSize:12]];
    CGRect startAmountFrame = cell.startAmountLabel.frame;
    cell.startAmountLabel.frame = CGRectMake(320-12-textLabelSize.width +20,startAmountFrame.origin.y, textLabelSize.width-20, startAmountFrame.size.height);
    
    cell.destinationAmountLabel.text = [NSString stringWithFormat:@"%d",[tempPlan.p_amount intValue]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys:[planArray objectAtIndex:indexPath.row],@"key", nil];
    
    if ( [isFromView isEqualToString:@"Add Income"]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"getPlanFromIncomeViewOnSuccess" object:tempDict];
    } else {
         [[NSNotificationCenter defaultCenter]postNotificationName:@"getPlanFromExpenseViewOnSuccess" object:tempDict];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
